package com.itx.gatewayx.controller;

import com.itx.gatewayx.common.R;
import com.itx.gatewayx.domain.NewsCategory;
import com.itx.gatewayx.service.NewsCategoryService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 新闻分类控制器
 *
 * @author itx
 */
@RestController
@RequestMapping("/api/news/categories")
@RequiredArgsConstructor
public class NewsCategoryController {
    
    private final NewsCategoryService categoryService;
    
    /**
     * 获取所有分类
     *
     * @return 分类列表
     */
    @GetMapping
    public R<List<NewsCategory>> getAllCategories() {
        List<NewsCategory> categories = categoryService.getAllCategories();
        return R.ok(categories);
    }
    
    /**
     * 根据ID获取分类
     *
     * @param id 分类ID
     * @return 分类信息
     */
    @GetMapping("/{id}")
    public R<NewsCategory> getCategoryById(@PathVariable String id) {
        NewsCategory category = categoryService.getCategoryById(id);
        return category != null ? R.ok(category) : R.notFound("分类不存在");
    }
    
    /**
     * 添加分类
     *
     * @param category 分类信息
     * @return 是否成功
     */
    @PostMapping
    public R<Boolean> addCategory(@RequestBody @Validated NewsCategory category) {
        boolean result = categoryService.addCategory(category);
        return result ? R.ok(true) : R.fail("添加分类失败");
    }
    
    /**
     * 更新分类
     *
     * @param id 分类ID
     * @param category 分类信息
     * @return 是否成功
     */
    @PutMapping("/{id}")
    public R<Boolean> updateCategory(@PathVariable String id, @RequestBody @Validated NewsCategory category) {
        category.setId(id);
        boolean result = categoryService.updateCategory(category);
        return result ? R.ok(true) : R.fail("更新分类失败");
    }
    
    /**
     * 删除分类
     *
     * @param id 分类ID
     * @return 是否成功
     */
    @DeleteMapping("/{id}")
    public R<Boolean> deleteCategory(@PathVariable String id) {
        boolean result = categoryService.deleteCategory(id);
        return result ? R.ok(true) : R.fail("删除分类失败");
    }
} 